/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.log.excel;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.excel.support.ExcelImporter;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.log.entity.IntegralLogEntity;
import org.springblade.modules.log.service.IIntegralLogService;
import org.springblade.modules.log.service.IIntegralYearLogService;
import org.springblade.modules.personal.entity.SecrecyLevel;
import org.springblade.modules.personal.entity.UserBaseEntity;
import org.springblade.modules.personal.service.IUserBaseService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 积分项数据导入类
 *
 * @author junxin
 */
@RequiredArgsConstructor
public class IntegralImporter implements ExcelImporter<IntegralLogExcel> {

	private final IIntegralLogService integralLogService;
	/**
	 * 首先需要排除重复导入的问题，每个学员在每个project_origin只能获得一次积分
	 * userBase表添加积分
	 */
	@Override
	@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
	public void save(List<IntegralLogExcel> dataList) {
		if (dataList.size()>5000)throw new ServiceException("导入条数不可超过5000条");
		String version= CommonUtil.getImportVersion();
		for (IntegralLogExcel data:dataList){
			//1.保存导入记录
			IntegralLogEntity integralLogEntity=new IntegralLogEntity();
			//1.1 查找该学员有无此项目的导入记录
			IntegralLogEntity oldIntegralLogEntity=integralLogService.getOne(Wrappers.<IntegralLogEntity>query().lambda()
				.eq(IntegralLogEntity::getUserName,data.getUserName())
				.eq(IntegralLogEntity::getProjectOrigin,data.getProjectOrigin())
				.eq(IntegralLogEntity::getIntegralOrigin,data.getIntegralOrigin()));
			if (null!= oldIntegralLogEntity) continue;
			//1.2 放入链表，后面批量保存
			BeanUtil.copyProperties(data,integralLogEntity);
			integralLogEntity.setVersion(version);
			//2.用户添加积分 和添加年度积分和学时
			integralLogService.addIntegralImport(integralLogService.generalIntegralByIntegral(integralLogEntity));
		}

	}


}
